home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch14
/
fig14_15.txt
< prev
Wrap
Text File
|
1998-02-27
|
7KB
|
208 lines
1 // Fig. 14.15: fig14_15.cpp
2 // This program reads a random access file sequentially,
3 // updates data already written to the file, creates new
4 // data to be placed in the file, and deletes data
5 // already in the file.
6 #include <iostream.h>
7 #include <fstream.h>
8 #include <iomanip.h>
9 #include <stdlib.h>
10 #include "clntdata.h"
11
12 int enterChoice();
13 void textFile( fstream& );
14 void updateRecord( fstream& );
15 void newRecord( fstream& );
16 void deleteRecord( fstream& );
17 void outputLine( ostream&, const clientData & );
18 int getAccount( const char * );
19
20 enum Choices { TEXTFILE = 1, UPDATE, NEW, DELETE, END };
21
22 int main()
23 {
24 fstream inOutCredit( "credit.dat", ios::in | ios::out );
25
26 if ( !inOutCredit ) {
27 cerr << "File could not be opened." << endl;
28 exit ( 1 );
29 }
30
31 int choice;
32
33 while ( ( choice = enterChoice() ) != END ) {
34
35 switch ( choice ) {
36 case TEXTFILE:
37 textFile( inOutCredit );
38 break;
39 case UPDATE:
40 updateRecord( inOutCredit );
41 break;
42 case NEW:
43 newRecord( inOutCredit );
44 break;
45 case DELETE:
46 deleteRecord( inOutCredit );
47 break;
48 default:
49 cerr << "Incorrect choice\n";
50 break;
51 }
52
53 inOutCredit.clear(); // resets end-of-file indicator
54 }
55
56 return 0;
57 }
58
59 // Prompt for and input menu choice
60 int enterChoice()
61 {
62 cout << "\nEnter your choice" << endl
63 << "1 - store a formatted text file of accounts\n"
64 << " called \\"print.txt\\" for printing\n"
65 << "2 - update an account\n"
66 << "3 - add a new account\n"
67 << "4 - delete an account\n"
68 << "5 - end program\n? ";
69
70 int menuChoice;
71 cin >> menuChoice;
72 return menuChoice;
73 }
74
75 // Create formatted text file for printing
76 void textFile( fstream &readFromFile )
77 {
78 ofstream outPrintFile( "print.txt", ios::out );
79
80 if ( !outPrintFile ) {
81 cerr << "File could not be opened." << endl;
82 exit( 1 );
83 }
84
85 outPrintFile << setiosflags( ios::left ) << setw( 10 )
86 << "Account" << setw( 16 ) << "Last Name" << setw( 11 )
87 << "First Name" << resetiosflags( ios::left )
88 << setw( 10 ) << "Balance" << endl;
89 readFromFile.seekg( 0 );
90
91 clientData client;
92 readFromFile.read( reinterpret_cast<char *>( &client ),
93 sizeof( clientData ) );
94
95 while ( !readFromFile.eof() ) {
96 if ( client.accountNumber != 0 )
97 outputLine( outPrintFile, client );
98
99 readFromFile.read( reinterpret_cast<char *>( &client ),
100 sizeof( clientData ) );
101 }
102 }
103
104 // Update an account's balance
105 void updateRecord( fstream &updateFile )
106 {
107 int account = getAccount( "Enter account to update" );
108
109 updateFile.seekg( ( account - 1 ) * sizeof( clientData ) );
110
111 clientData client;
112 updateFile.read( reinterpret_cast<char *>( &client ),
113 sizeof( clientData ) );
114
115 if ( client.accountNumber != 0 ) {
116 outputLine( cout, client );
117 cout << "\nEnter charge (+) or payment (-): ";
118
119 float transaction; // charge or payment
120 cin >> transaction; // should validate
121 client.balance += transaction;
122 outputLine( cout, client );
123 updateFile.seekp( ( account-1 ) * sizeof( clientData ) );
124 updateFile.write(
125 reinterpret_cast<const char *>( &client ),
126 sizeof( clientData ) );
127 }
128 else
129 cerr << "Account #" << account
130 << " has no information." << endl;
131 }
132
133 // Create and insert new record
134 void newRecord( fstream &insertInFile )
135 {
136 int account = getAccount( "Enter new account number" );
137
138 insertInFile.seekg( ( account-1 ) * sizeof( clientData ) );
139
140 clientData client;
141 insertInFile.read( reinterpret_cast<char *>( &client ),
142 sizeof( clientData ) );
143
144 if ( client.accountNumber == 0 ) {
145 cout << "Enter lastname, firstname, balance\n? ";
146 cin >> client.lastName >> client.firstName
147 >> client.balance;
148 client.accountNumber = account;
149 insertInFile.seekp( ( account - 1 ) *
150 sizeof( clientData ) );
151 insertInFile.write(
152 reinterpret_cast<const char *>( &client ),
153 sizeof( clientData ) );
154 }
155 else
156 cerr << "Account #" << account
157 << " already contains information." << endl;
158 }
159
160 // Delete an existing record
161 void deleteRecord( fstream &deleteFromFile )
162 {
163 int account = getAccount( "Enter account to delete" );
164
165 deleteFromFile.seekg( (account-1) * sizeof( clientData ) );
166
167 clientData client;
168 deleteFromFile.read( reinterpret_cast<char *>( &client ),
169 sizeof( clientData ) );
170
171 if ( client.accountNumber != 0 ) {
172 clientData blankClient = { 0, "", "", 0.0 };
173
174 deleteFromFile.seekp( ( account - 1) *
175 sizeof( clientData ) );
176 deleteFromFile.write(
177 reinterpret_cast<const char *>( &blankClient ),
178 sizeof( clientData ) );
179 cout << "Account #" << account << " deleted." << endl;
180 }
181 else
182 cerr << "Account #" << account << " is empty." << endl;
183 }
184
185 // Output a line of client information
186 void outputLine( ostream &output, const clientData &c )
187 {
188 output << setiosflags( ios::left ) << setw( 10 )
189 << c.accountNumber << setw( 16 ) << c.lastName
190 << setw( 11 ) << c.firstName << setw( 10 )
191 << setprecision( 2 ) << resetiosflags( ios::left )
192 << setiosflags( ios::fixed | ios::showpoint )
193 << c.balance << '\n';
194 }
195
196 // Get an account number from the keyboard
197 int getAccount( const char *prompt )
198 {
199 int account;
200
201 do {
202 cout << prompt << " (1 - 100): ";
203 cin >> account;
204 } while ( account < 1 || account > 100 );
205
206 return account;
207 }